<template>
  <div>
    <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="100px" class="form-style">
      <aside>Create or Update a Online Tool.</aside>
      <el-form-item label="Category" prop="category">
        <DictGroupInput ref="dictGroupInput" :value.sync="form.category" dict-group="tool_category" placeholder="Category" />
      </el-form-item>
      <el-form-item label="Tool Name" prop="toolName">
        <el-input v-model="form.toolName" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="Turn To" prop="turnTo">
        <el-input v-model="form.turnTo" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="Icon" prop="icon">
        <el-input v-model="form.icon" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="Standby" prop="standby">
        <el-input v-model="form.standby" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="State" prop="state">
        <el-input v-model="form.state" :autosize="{ minRows: 3, maxRows: 5 }" type="textarea" placeholder="Please input" />
      </el-form-item>
      <el-form-item>
        <el-button v-waves v-loading="loading" type="primary" @click="submitForm">Save Now</el-button>
        <el-button v-show="!isEdit" @click="reset">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { createOrUpdateOnlineTool } from '@/api/util/online-tool'
import waves from '@/directive/waves' // waves directive
import DictGroupInput from '@/views/components/DictGroupInput'

const defaultForm = {
  category: '',
  toolName: '',
  turnTo: '',
  icon: '',
  standby: '',
  state: ''
}

export default {
  name: 'OnlineTool',
  components: { DictGroupInput },
  directives: { waves },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      rules: {
        toolName: [{ required: true, message: 'Tool Name is required', trigger: 'blur' }],
        category: [{ required: true, message: 'category is required', trigger: 'change' }],
        turnTo: [{ required: true, message: 'Turn To is required', trigger: 'blur' }],
        icon: [{ required: true, message: 'Icon To is required', trigger: 'blur' }]
      },
      loading: false
    }
  },
  computed: {
    ...mapGetters(['uid'])
  },
  created() {
    if (this.isEdit) {
      const row = this.$route.params
      this.form = Object.assign({}, row, { category: row.category.toString() }, { createAt: undefined, modifyAt: undefined })
    } else {
      this.form = Object.assign({}, defaultForm, { uid: this.uid })
    }
  },
  methods: {
    submitForm() {
      console.log(this.form)
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.loading = true
          createOrUpdateOnlineTool(this.form)
            .then(response => {
              this.$notify({
                title: 'Success',
                message: 'save successfully',
                type: 'success',
                duration: 2000
              })
              this.$refs.dictGroupInput.$emit('newDict', this.form.category)
              if (!this.isEdit) {
                this.reset()
              }
              this.$refs.dictGroupInput.$emit('reset', this.form.category)
            })
            .catch(() => {
              this.loading = false
            })
          this.loading = false
        } else {
          return false
        }
      })
    },
    reset() {
      this.$refs['dataForm'].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.form-style {
  margin-top: 1%;
  width: 88%;
  margin-left: 6%;
}
</style>
